const callServerIp = localStorage.getItem('call_server') === null ? '' : JSON.parse(localStorage.getItem('call_server')).ip;
const callServerPort = localStorage.getItem('call_server') === null ? '' : JSON.parse(localStorage.getItem('call_server')).port;
const deviceId = localStorage.getItem('deviceId');
const id = localStorage.getItem('id');

const Socket = () => {
    let socket;

    //let webSocketUrl = 'wss://10.181.255.203/call/' + id;
    let webSocketUrl = 'ws://' + callServerIp + ":" + callServerPort + '/call/station/' + id + '/' + deviceId;

    if ('WebSocket' in window && deviceId !== null) {
        socket = new WebSocket(webSocketUrl);
    } else if (deviceId === null) {
        return;
    } else {
        console.error('请检查浏览器是否支持websocket！');
    }

    //连接成功
    socket.onopen = function () {
        console.log('websocket连接成功！');
    };
    //连接关闭
    socket.onclose = function (e) {
        console.log('websocket连接关闭！'+ e.code + ' ' + e.reason + ' ' + e.wasClean);
    };
    //连接出错
    socket.onerror = function (e) {
        console.log(e);
        console.error('websocket连接失败！');
    };
    //连接通信
    //socket.onmessage = onmessage

    return socket
}

export {
    Socket, deviceId
}
//
// export class Socket {
// }